﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infraestructure.CrossCutting.Security;
using Infraestructure.CrossCutting.NetFramework.Resources;
using System.Web.Security;

namespace Infraestructure.CrossCutting.NetFramework.Security
{
    public sealed class RoleManager
        : IRoleManager
    {

        public void CreateRole(string roleName)
        {
            if (String.IsNullOrEmpty(roleName))
            {
                throw new ArgumentNullException("roleName", Messages.exception_InvalidRoleName);
            }

            Roles.CreateRole(roleName);
        }

        public bool DeleteRole(string roleName)
        {
            if (String.IsNullOrEmpty(roleName))
            {
                throw new ArgumentNullException("roleName", Messages.exception_InvalidRoleName);
            }

            return Roles.DeleteRole(roleName);
        }

        public string[] FindUsersInRole(string roleName, string username)
        {
            if (String.IsNullOrEmpty(roleName))
            {
                throw new ArgumentNullException("roleName", Messages.exception_InvalidRoleName);
            }

            if (String.IsNullOrEmpty(username))
            {
                throw new ArgumentNullException("username", Messages.exception_InvalidUserName);
            }

            return Roles.FindUsersInRole(roleName, username);
        }

        public bool IsUserInRole(string username, string roleName)
        {
            if (String.IsNullOrEmpty(username))
            {
                throw new ArgumentNullException("username", Messages.exception_InvalidUserName);
            }
            
            if (String.IsNullOrEmpty(roleName))
            {
                throw new ArgumentNullException("roleName", Messages.exception_InvalidRoleName);
            }

            return Roles.IsUserInRole(username, roleName);

        }

        public string[] GetUsersInRole(string roleName)
        {
            if (String.IsNullOrEmpty(roleName))
            {
                throw new ArgumentNullException("roleName", Messages.exception_InvalidRoleName);
            }

            return Roles.GetUsersInRole(roleName);
        }

        public string[] GetRolesForUser(string username)
        {
            if (String.IsNullOrEmpty(username))
            {
                throw new ArgumentNullException("username", Messages.exception_InvalidUserName);
            }

            return Roles.GetRolesForUser(username);

        }

        public string[] GetAllRoles()
        {
            return Roles.GetAllRoles();
        }

        public bool RoleExists(string roleName)
        {
            if (String.IsNullOrEmpty(roleName))
            {
                throw new ArgumentNullException("roleName", Messages.exception_InvalidRoleName);
            }

            return Roles.RoleExists(roleName);

        }
    }
}
